package enterprise.manage.security.utils;
 
import org.springframework.security.providers.encoding.Md5PasswordEncoder;
import org.springframework.security.providers.encoding.PasswordEncoder;


/**
 * Created by IntelliJ IDEA.
 * User: zhenmingyue
 * Date: 2011-7-7 15:47:10
 * Desc:
 * For :
 */
public class PasswordUtils {

    /**
     * 允许继承的构造函数
     */
    protected PasswordUtils() {
    }

    /**
     * 根据用户名和密码明文获得加密后的密码
     *
     * @param username      用户名
     * @param plainPassword 密码明文
     * @return 加密后的密码
     */
    public static String passwordEncrypt(String username, String plainPassword) {
        PasswordEncoder passwordEncoder = new Md5PasswordEncoder();
        return passwordEncoder.encodePassword(plainPassword, username);
    }

    /**
     * 判断明文密码是否正确
     *
     * @param username      用户名
     * @param plainPassword 明文密码
     * @param password      加密后的密码
     * @return 明文密码加密后是否等于正确密码
     */
    public static boolean isPasswordValid(String username, String plainPassword, String password) {
        return password.equals(passwordEncrypt(username, plainPassword));
    }


    public static void main(String[] args) {
        String userName;
        String userPassword ;
        userName = "test@shenzhoufu.com";
        userPassword = "time9818";
        userPassword = PasswordUtils.passwordEncrypt(userName, userPassword);
        System.out.println("userPassword:" + userPassword);
    }
}
